package com.xiaojie.demo.service.impl;

import com.xiaojie.demo.entity.Account;
import com.xiaojie.demo.mapper.AccountMapper;
import com.xiaojie.demo.service.TransferService;
import com.xiaojie.summer.annotation.Autowired;
import com.xiaojie.summer.annotation.Service;
import com.xiaojie.summer.annotation.Transactional;

import java.util.List;

/**
 * @author Nick
 * @date 2021/5/22
 * @description 转账实现类
 */
@Service("transferService")
@Transactional(rollbackFor = "e")
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) {

        Account from = new Account();
        from.setCardNo(fromCardNo);
        Account to = new Account();
        to.setCardNo(toCardNo);
        List<Account> formList = (List<Account>) accountMapper.queryAccountByCardNo(from);
        from = formList.get(0);
        List<Account> toList = (List<Account>) accountMapper.queryAccountByCardNo(to);
        to = toList.get(0);

        from.setMoney(from.getMoney() - money);
//        int a = 1 /0;
        to.setMoney(to.getMoney() + money);

        accountMapper.updateAccountByCardNo(to);
        accountMapper.updateAccountByCardNo(from);

    }

    @Override
    public String toString() {
        return "TransferServiceImpl{" +
                "accountMapper=" + accountMapper +
                '}';
    }
}
